package com.zbkj.crmeb.agent.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.agent.model.AgentFreezeBill;
import com.zbkj.crmeb.agent.dao.AgentFreezeBillDao;
import com.zbkj.crmeb.agent.service.AgentFreezeBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.crmeb.bill.model.MerFreezeBill;
import com.zbkj.crmeb.store.model.StoreOrderInfo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 服务商冻结账单表 服务实现类
 * </p>
 *
 * @author wuzp
 * @since 2022-02-18
 */
@Service
public class AgentFreezeBillServiceImpl extends ServiceImpl<AgentFreezeBillDao, AgentFreezeBill> implements AgentFreezeBillService {

    @Override
    public void updateAmountFrozen(StoreOrderInfo storeOrderInfo, BigDecimal firstAgentFrozen) {
        //修改冻结账单
        AgentFreezeBill agentFreezeBill = getOne(Wrappers.lambdaQuery(AgentFreezeBill.class).eq(AgentFreezeBill::getOrderId, storeOrderInfo.getOrderId())
                .eq(AgentFreezeBill::getFrozen,0).last("LIMIT 1"));
        if(agentFreezeBill == null ){
            throw new CrmebException("冻结账单不存在");
        }
        if(firstAgentFrozen.compareTo(BigDecimal.ZERO)>0){
            agentFreezeBill.setAmountFrozen(agentFreezeBill.getAmountFrozen().subtract(firstAgentFrozen));
            updateById(agentFreezeBill);
        }
    }
}
